package dao.entity;

import bean.Order;
import dao.MysqlConfiguration;
import dao.interf.OrderDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    @Override
    public int addOrder(Order c) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        //定义sql语句
        String sql = "insert into orders values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        int n = 0;//受影响的行数
        try {
            //创建statement语句对象
            //Statement st = conn.createStatement();
            //创建一个PrepareStatement语句对象
            PreparedStatement pst = conn.prepareStatement(sql);
            System.out.println("创建语句对象成功!");
            pst.setString(1,c.getOid());
            pst.setString(2,c.getOrder_time());
            pst.setString(3,c.getOrder_dest());
            pst.setString(4,c.getOrder_from());
            pst.setString(5,c.getMid());
            pst.setString(6,c.getMname());
            pst.setString(7,c.getCid());
            pst.setString(8,c.getCname());
            pst.setInt(9,c.getO_number());
            pst.setString(10,c.getC_phone());
            pst.setString(11,c.getOrder_type());
            pst.setString(12,c.getEid());
            pst.setDouble(13,c.getTweight());
            pst.setString(14,c.getO_status());
            n = pst.executeUpdate();//执行语句
            System.out.println("执行语句成功!");
            //执行指定的sql语句
            //n = pst.executeUpdate();
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return n;//返回受影响的行数
    }

    @Override
    public int updateOrder(Order c) {
        return 0;
    }

    @Override
    public int selectOidmaxOrder() {
        //连接数据库
        String oid = "";
        int num = 0;
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select  max(oid) from orders;";
        ResultSet rs = null;
        try {
            Statement st = conn.createStatement();
            System.out.println("查询订单语句创建成功!");
            rs = st.executeQuery(sql);
            if(rs!=null){
                System.out.println("查找成功");
                rs.next();//移动光标
                System.out.println("光标移动成功");
                oid = rs.getString(1);
                System.out.println("查询成功,oid为"+oid);
                oid = oid.substring(1);
                num = Integer.parseInt(oid);
                System.out.println("获取成功,oid序号为"+ num);
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return num;
    }

    @Override
    public ResultSet queryOrder() {
        return null;
    }

    @Override
    public List<Order> order_find(int currentPage) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select * from orders limit ?,?;";
        ResultSet rs;
        List<Order> list = new ArrayList<Order>();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setInt(1,(currentPage-1)*5);
            pst.setInt(2,5);
            rs = pst.executeQuery();
            while (rs.next()){
                Order o = new Order();
                o.setOid(rs.getString(1));
                o.setOrder_time(rs.getString(2));
                o.setOrder_dest(rs.getString(3));
                o.setOrder_from(rs.getString(4));
                o.setMid(rs.getString(5));
                o.setMname(rs.getString(6));
                o.setCid(rs.getString(7));
                o.setCname(rs.getString(8));
                o.setO_number(Integer.parseInt(rs.getString(9)));
                o.setC_phone(rs.getString(10));
                o.setOrder_type(rs.getString(11));
                o.setEid(rs.getString(12));
                o.setTweight(Double.parseDouble(rs.getString(13)));
                o.setO_status(rs.getString(14));
                list.add(o);//添加当前学生到列表中
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败!"+e.getMessage());
        }
        return list;
    }

    @Override
    public int Order_count() {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        String sql = "select count(*) from orders;";
        ResultSet rs;
        int n = 0;
        try {
            Statement st = conn.createStatement();
            rs = st.executeQuery(sql);
            while (rs.next()){
                n = rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println("创建语句对象失败! 错误是:"+e.getMessage());
        }
        return n;
    }

    @Override
    public int deleteOrder(String oid) {
        //连接数据库
        Connection conn = MysqlConfiguration.MysqlConnection();
        //定义sql语句
        String sql = "delete from orders where oid=?";
        int n = 0;//初始化受影响的行数
        try {
            //定义预处理语句对象
            PreparedStatement pst = conn.prepareStatement(sql);
            System.out.println("创建语句对象成功");
            pst.setString(1,oid);
            //pst.setInt(1,sno);
            n = pst.executeUpdate();//执行语句
            //System.out.println("执行语句成功");
        } catch (Exception e) {
            System.out.println("创建语句异常: "+e.getMessage());
        }
        return n;
    }

    @Override
    public ResultSet queryOrderByOid(String ccid) {
        return null;
    }
}
